package com.mairuide.phone.eachothercall;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.SQLQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mairuide._frame.base.BaseRepository;
import com.mairuide._frame.base.BaseServiceProxy;
import com.mairuide._frame.utils.StringUtils;
import com.mairuide._frame.utils.Testtrans;
import com.mairuide.httpclient.BaseSite;
import com.mairuide.phone.PhoneRecord;
import com.mairuide.phone.callanalysis.entity.PhoneMapDetail;
import com.mairuide.phone.trackevents.TrackEventsService;


@Service
@Transactional(readOnly = true)
public class EachOtherCallAnalysisService extends BaseServiceProxy<PhoneRecord> {

	@PersistenceContext
	private EntityManager entityManager;
	@Resource
	private BaseSite baseSite;
	@Resource
	private TrackEventsService trackEventsService;

	@Override
	protected EntityManager getEntityManager() {
		return entityManager;
	}

	@Override
	protected BaseRepository<PhoneRecord> getBaseRepository() {
		return null;
	}
	
	/**
	 * 相互通话分析入口
	 * @param eocap
	 * @return
	 */
	public List<EachOtherCallAnalysis> getEachOtherCallAnalysisList(EachOtherCallAnalysisParams eocap) {
		List<EachOtherCallAnalysis> ecas = new ArrayList<EachOtherCallAnalysis>();
		//相互通话分析查询
		List<PhoneRecord> prs = findEachOtherCall(eocap);
		String[] strs = eocap.getCheckedPhone().split(",");
		for(String phoneNumber : strs) {
			if(StringUtils.isEmpty(phoneNumber)) {
				continue;
			}
			EachOtherCallAnalysis eca = new EachOtherCallAnalysis();
			List<EachOtherCallAnalysisPersonal> ecaps = new ArrayList<EachOtherCallAnalysisPersonal>();
			List<Timestamp> beginTimes = new ArrayList<Timestamp>();
			List<Timestamp> endTimes = new ArrayList<Timestamp>();
			for(String phoneNumber2 : strs) {
				//如果号码相等则不核算
				if(phoneNumber.equals(phoneNumber2)) {
					EachOtherCallAnalysisPersonal ecap = new EachOtherCallAnalysisPersonal();
					ecap.setBeginTime(null);
					ecap.setCallNumber(0);
					ecap.setCallSeconds(0);
					ecap.setEndTime(null);
					ecap.setSuspectPhonePersonal(phoneNumber2);
					ecaps.add(ecap);
					continue;
				}
				Integer callNumber = 0;//通话次数
				Integer caller = 0;//主叫
				Integer called = 0;//被叫
				Integer callSeconds = 0;//通话秒数
				Timestamp beginTime = null;//开始时间
				int ix = 0;
				for(PhoneRecord pr : prs) {
					if((phoneNumber.equals(pr.getServiceNumber()) && phoneNumber2.equals(pr.getPartyNumber())) ||
							(phoneNumber.equals(pr.getPartyNumber()) && phoneNumber2.equals(pr.getServiceNumber()))) {
						callNumber ++;//通话次数
						if(pr.getCallType()==0){
							caller++;
						}else {
							called++;
						}
						if(StringUtils.isNotEmpty(pr.getTalkTime())) {
							callSeconds += Integer.parseInt(pr.getTalkTime());//通话秒数
						}
						if(ix == 0) {//开始时间
							beginTime = pr.getStartingTime();
						}
						ix++;
					}
				}
				EachOtherCallAnalysisPersonal ecap = new EachOtherCallAnalysisPersonal();
				ecap.setSuspectPhonePersonal(phoneNumber2);
				ecap.setCallNumber(callNumber);
				ecap.setCallSeconds(callSeconds);
				ecap.setCaller(caller);
				ecap.setCalled(called);
				ecap.setBeginTime(beginTime);
				Timestamp endTime = StringUtils.addM4TimeStamp(beginTime, callSeconds);//计算结束时间
				beginTimes.add(beginTime);
				endTimes.add(endTime);
				ecap.setEndTime(endTime);
				//条件
				addWhere4CallNumberAndStartTimeAndEndTime(eocap, eca, ecaps, ecap, phoneNumber2);
			}
			//总通话次数
			Integer totalCallNumber = 0;
			//总通话秒数
			Integer totalCallSeconds = 0;
			for(EachOtherCallAnalysisPersonal ecap : ecaps) {
				totalCallNumber += ecap.getCallNumber();
				totalCallSeconds += ecap.getCallSeconds();
			}
			//总开始时间
			Timestamp totalBeginTime = StringUtils.getMinBeginTime(beginTimes);
			//总结束时间
			Timestamp totalEndTime = StringUtils.getMaxBeginTime(endTimes);
			eca.setTotalBeginTime(totalBeginTime);
			eca.setTotalEndTime(totalEndTime);
			eca.setSuspectPhone(phoneNumber);
			eca.setTotalCallNumber(totalCallNumber);
			eca.setTotalCallSeconds(totalCallSeconds);
			ecas.add(eca);
		}
		return ecas;
	}
	public void getD3Result(EachOtherCallAnalysisParams eocap) {
		//相互通话分析查询
//		List<PhoneRecord> prs = findEachOtherCall(eocap);
//		List<PhoneRecord> prs2 = new ArrayList<>(prs);
//		if(null != prs && !prs.isEmpty()) {
//			EachOtherCallAnalysisPersonal ecap = new EachOtherCallAnalysisPersonal();
//			for(PhoneRecord pr :prs){
//				int count_number=0;
//				for(PhoneRecord pr2 :prs2){
//					if(pr.getServiceNumber().equals(pr2.getPartyNumber()) && pr.getPartyNumber().equals(pr2.getServiceNumber())){
//						prs.remove(pr2);
//						count_number++;
//						continue;
//					}
//				}
//				ecap.setCallNumber(count_number);
//				ecap.setSuspectPhonePersonal(pr.getServiceNumber());
//			}
//		}
	}
	/**
	 * 查询条件添加
	 * @param eocap
	 * @param eca
	 * @param ecaps
	 * @param ecap
	 * @param phoneNumber2
	 */
	private void addWhere4CallNumberAndStartTimeAndEndTime(EachOtherCallAnalysisParams eocap, EachOtherCallAnalysis eca,
			List<EachOtherCallAnalysisPersonal> ecaps, EachOtherCallAnalysisPersonal ecap, String phoneNumber2) {
		//通话次数查询、通话时间条件
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startTime = eocap.getStartTime();
		String endTime = eocap.getEndTime();
		try {
			if (org.apache.commons.lang3.StringUtils.isNotBlank(startTime)){
				java.util.Date date = format1.parse(startTime);
				startTime=format2.format(date);
			}
			if (org.apache.commons.lang3.StringUtils.isNotBlank(endTime)){
				java.util.Date date2 = format1.parse(endTime);
				endTime=format2.format(date2);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}


		if(null != eocap.getCallNumber() || StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
			//判断三个条件都不为空时添加条件
			if(eocap.getCallNumber() !=null && eocap.getCallNumber() >= 0 && StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
				if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
					if(ecap.getCallNumber() >= eocap.getCallNumber() 
							&& (ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime)))
						    && (ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime)))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else if(StringUtils.isNotEmpty(startTime)) {
					if(ecap.getCallNumber() >= eocap.getCallNumber() 
							&& (ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime)))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else if(StringUtils.isNotEmpty(endTime)) {
					if(ecap.getCallNumber() >= eocap.getCallNumber() 
							&& (ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime)))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}
				
			//判断通话次数不为空通话开始时间不为空
			}else if(eocap.getCallNumber() !=null && eocap.getCallNumber() >= 0 && StringUtils.isNotEmpty(startTime)) {
				if(ecap.getCallNumber() >= eocap.getCallNumber() 
						&& (ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime)))) {
					ecaps.add(ecap);
					eca.setEcaps(ecaps);
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			//判断通话次数不为空通话结束时间不为空
			}else if(eocap.getCallNumber() !=null && eocap.getCallNumber() >= 0 && StringUtils.isNotEmpty(endTime)) {
				if(ecap.getCallNumber() >= eocap.getCallNumber() 
						&& (ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime)))) {
					ecaps.add(ecap);
					eca.setEcaps(ecaps);
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			//判断通话开始时间、通话结束时间不为空
			}else if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
				if(ecap.getBeginTime() != null && ecap.getEndTime()!= null) {
					if((ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime)))
							&& (ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime)))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else if(ecap.getBeginTime() != null) {
					if(ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else if(ecap.getEndTime() != null) {
					if(ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			//判断通话开始时间不为空
			}else if(StringUtils.isNotEmpty(startTime)) {
				if(ecap.getBeginTime() != null) {
					if(ecap.getBeginTime().after(Timestamp.valueOf(startTime)) || ecap.getBeginTime().equals(Timestamp.valueOf(startTime))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			//判断通话结束时间不为空
			}else if(StringUtils.isNotEmpty(endTime)) {
				if(ecap.getEndTime() != null) {
					if(ecap.getEndTime().before(Timestamp.valueOf(endTime)) || ecap.getEndTime().equals(Timestamp.valueOf(endTime))) {
						ecaps.add(ecap);
						eca.setEcaps(ecaps);
					}else {
						addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
					}
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			//判断通话次数不为空
			}else if(eocap.getCallNumber() !=null && eocap.getCallNumber() >= 0) {
				if(ecap.getCallNumber() >= eocap.getCallNumber()) {
					ecaps.add(ecap);
					eca.setEcaps(ecaps);
				}else {
					addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
				}
			}else {
				addEmptyEachOtherCallAnalysisPersonal(eca, ecaps, ecap, phoneNumber2);
			}
		}else {
			ecaps.add(ecap);
			eca.setEcaps(ecaps);
		}
	}
	
	/**
	 * 添加
	 * @param eca
	 * @param ecaps
	 * @param ecap
	 * @param phoneNumber2
	 */
	private void addEmptyEachOtherCallAnalysisPersonal(EachOtherCallAnalysis eca,
			List<EachOtherCallAnalysisPersonal> ecaps, EachOtherCallAnalysisPersonal ecap, String phoneNumber2) {
		ecap.setBeginTime(null);
		ecap.setCallNumber(0);
		ecap.setCallSeconds(0);
		ecap.setEndTime(null);
		ecap.setSuspectPhonePersonal(phoneNumber2);
		ecaps.add(ecap);
		eca.setEcaps(ecaps);
	}
	
	/**
	 * 相互通话分析查询
	 * @param eocap
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PhoneRecord> findEachOtherCall(EachOtherCallAnalysisParams eocap) {
		StringBuffer selectSql = new StringBuffer();
		selectSql.append("SELECT\r\n" + 
				"  TPR.RECORD_ID AS recordId,\r\n" + 
				"  TPR.CASEMANAGE_ID AS casemanageId,\r\n" + 
				"  TPR.STARTING_TIME AS startingTime,\r\n" + 
				"  TPR.SERVICE_NUMBER AS serviceNumber,\r\n" + 
				"  TPR.SERVICE_USER_INFORMATION AS serviceUserInformation,\r\n" + 
				"  TPR.SERVICE_LOCATION AS serviceLocation,\r\n" + 
				"  TPR.CARD_NUMBER AS cardNumber,\r\n" + 
				"  TPR.PARTY_NUMBER AS partyNumber,\r\n" + 
				"  TPR.PARTY_USER_INFORMATION AS partyUserInformation,\r\n" + 
				"  TPR.PARTY_LOCATION AS partyLocation,\r\n" + 
				"  TPR.TALK_TIME AS talkTime,\r\n" + 
				"  TPR.CALL_TYPE AS callType,\r\n" + 
				"  TPR.LAC AS lac,\r\n" + 
				"  TPR.CID AS cid,\r\n" + 
				"  TPR.BASE_STATION AS baseStation,\r\n" + 
				"  TPR.MSC AS msc,\r\n" + 
				"  TPR.CITY AS city,\r\n" + 
				"  TPR.THIRD_NUMBER AS thirdNumber,\r\n" + 
				"  TPR.THIRD_NUMBER_INFORMATION AS thirdNumberInformation,\r\n" + 
				"  TPR.THIRD_NUMBER_LOCATION AS thirdNumberLocation,\r\n" + 
				"  TPR.OPERATOR_CODE AS operationCode,\r\n" + 
				"  TPR.OPERATOR_CODE_INFORMATION AS operationCodeInformation,\r\n" + 
				"  TPR.BUSINESS_KEY AS businessKey\r\n" + 
				"FROM\r\n" + 
				"  TB_PHONE_RECORDING TPR\r\n" + 
				"WHERE\r\n" + 
				"TPR.IS_DELETE=0 AND TPR.CALL_TYPE IN(0,1) ");
		
		/* 案件id */
		if(StringUtils.isNotEmpty(eocap.getCaseId())) {
			selectSql.append("AND TPR.CASEMANAGE_ID ='"+eocap.getCaseId()+"' ");
		}
		/* 主叫号码 、被叫号码*/
		selectSql.append(" AND (1=0 ");
		if(StringUtils.isNotEmpty(eocap.getCheckedPhone())) {
			String phones = "";
			for(String str : eocap.getCheckedPhone().split(",")) {
				if(StringUtils.isNotEmpty(str)) {
					phones = phones+"'"+str+"',";
				}
			}
			if(phones.endsWith(",")) {
				phones = phones.substring(0,phones.length()-1);
			}
			if(phones.length()>0) {
				selectSql.append(" OR (TPR.SERVICE_NUMBER IN ("+phones+")");
				selectSql.append(" AND TPR.PARTY_NUMBER IN ("+phones+"))");
			}
		}
		selectSql.append(" ) ");
		selectSql.append(" ORDER BY TPR.STARTING_TIME ");
		Query query = getEntityManager().createNativeQuery(selectSql.toString());
		query.unwrap(SQLQuery.class).setResultTransformer(new Testtrans(PhoneRecord.class));
		//封装返回数据
		return query.getResultList();
	}
	
	/**
	 * 相互通话详情查询
	 * @param eocap
	 * @return
	 */
	public Page<PhoneRecord> eachOtherDetails(EachOtherCallAnalysisParams eocap, Pageable page,String sortName,String sortOrder) {
		List<Object> params = new LinkedList<Object>();
		StringBuffer selectSql = new StringBuffer();
		selectSql.append("SELECT\r\n" + 
				"  TPR.RECORD_ID AS recordId,\r\n" + 
				"  TPR.CASEMANAGE_ID AS casemanageId,\r\n" + 
				"  TPR.STARTING_TIME AS startingTime,\r\n" + 
				"  TPR.SERVICE_NUMBER AS serviceNumber,\r\n" + 
				"  TPR.SERVICE_USER_INFORMATION AS serviceUserInformation,\r\n" + 
				"  TPR.SERVICE_LOCATION AS serviceLocation,\r\n" + 
				"  TPR.CARD_NUMBER AS cardNumber,\r\n" + 
				"  TPR.PARTY_NUMBER AS partyNumber,\r\n" + 
				"  TPR.PARTY_USER_INFORMATION AS partyUserInformation,\r\n" + 
				"  TPR.PARTY_LOCATION AS partyLocation,\r\n" + 
				"  TPR.TALK_TIME AS talkTime,\r\n" + 
				"  TPR.CALL_TYPE AS callType,\r\n" + 
				"  TPR.LAC AS lac,\r\n" +
				"  TPR.ADDRESS AS address,\r\n" +
				"  TPR.CID AS cid,\r\n" + 
				"  TPR.BASE_STATION AS baseStation,\r\n" + 
				"  TPR.MSC AS msc,\r\n" + 
				"  TPR.CITY AS city,\r\n" + 
				"  TPR.THIRD_NUMBER AS thirdNumber,\r\n" + 
				"  TPR.THIRD_NUMBER_INFORMATION AS thirdNumberInformation,\r\n" + 
				"  TPR.THIRD_NUMBER_LOCATION AS thirdNumberLocation,\r\n" + 
				"  TPR.OPERATOR_CODE AS operationCode,\r\n" + 
				"  TPR.OPERATOR_CODE_INFORMATION AS operationCodeInformation,\r\n" + 
				"  TPR.BUSINESS_KEY AS businessKey\r\n");
		StringBuffer fromWhereSql = new StringBuffer();
		fromWhereSql.append("FROM\r\n" + 
				"  TB_PHONE_RECORDING TPR\r\n" + 
				"WHERE\r\n" + 
				"TPR.IS_DELETE = 0 AND TPR.CALL_TYPE IN(0,1) ");
		
		/* 案件id */
		if(StringUtils.isNotEmpty(eocap.getCaseId())) {
			fromWhereSql.append("AND TPR.CASEMANAGE_ID =? ");
			params.add(eocap.getCaseId());
		}
		/* 主叫被叫 */
		fromWhereSql.append(" AND (1=0 ");
		if(StringUtils.isNotEmpty(eocap.getSuspectPhone()) && StringUtils.isNotEmpty(eocap.getSuspectPhonePersonal())) {
			fromWhereSql.append("OR TPR.SERVICE_NUMBER IN('"+eocap.getSuspectPhone()+"','"+eocap.getSuspectPhonePersonal()+"') ");
			fromWhereSql.append("AND TPR.PARTY_NUMBER IN('"+eocap.getSuspectPhone()+"','"+eocap.getSuspectPhonePersonal()+"') ");
		}
		fromWhereSql.append(" ) ");
		StringBuffer orderBySql = new StringBuffer();
		if(StringUtils.isNotEmpty(sortName)) {
			orderBySql.append(" ORDER BY "+sortName+ " " +sortOrder);
		}else {
			orderBySql.append(" ORDER BY TPR.STARTING_TIME DESC ");
		}
		//封装返回数据
		Page<PhoneRecord> pageCall = super.findPageBySql(PhoneRecord.class,selectSql.toString(), fromWhereSql.toString(), orderBySql.toString(), params, page);
		getAddress(pageCall);
		return pageCall;
	}

	private void getAddress(Page<PhoneRecord> pageCall){
		Set<PhoneMapDetail> phoneMapDetailList = new HashSet<>();
		List<String> phoneCodeList = new ArrayList<String>();
		if(pageCall!=null && pageCall.getContent()!=null) {
			for(PhoneRecord pr:pageCall.getContent()) {
				if(pr.getPartyLocation()!=null && isNumeric(pr.getPartyLocation())) {//判断对方号码归属地是否是电话区号
					String partyLocation = pr.getPartyLocation().trim();
					if(!phoneCodeList.contains(partyLocation)) {
						phoneCodeList.add(partyLocation);
					}
				}
				if(StringUtils.isEmpty(pr.getAddress()) && StringUtils.isNotEmpty(pr.getCid()) && StringUtils.isNotEmpty(pr.getLac())) {
					PhoneMapDetail detail = new PhoneMapDetail();
					detail.setCID(StringUtils.isEmpty(pr.getCid())?"0":pr.getCid());
					detail.setLAC(StringUtils.isEmpty(pr.getLac())?"0":pr.getLac());
					detail.setLATB(StringUtils.isEmpty(pr.getLatb())?"0":pr.getLatb());
					detail.setLNGB(StringUtils.isEmpty(pr.getLngb())?"0":pr.getLngb());
					detail.setADDRESS(StringUtils.isEmpty(pr.getAddress())?"0":pr.getAddress());
					detail.setSERVICE_NUMBER(pr.getServiceNumber());
					phoneMapDetailList.add(detail);
				}
			}
		}
		if(phoneCodeList.size()>0) {
			Map<String, String> phoneCodeMap = trackEventsService.getLocationByPhoneCode(phoneCodeList);
			for(PhoneRecord pr:pageCall.getContent()) {
				if(pr.getPartyLocation()!=null && isNumeric(pr.getPartyLocation())) {//判断对方号码归属地是否是电话区号
					String partyLocation = pr.getPartyLocation().trim();
					if(phoneCodeMap.get(partyLocation)!=null) {
						pr.setPartyLocation(phoneCodeMap.get(partyLocation).toString());
					}
				}
			}
		}
		List<PhoneMapDetail> arrayList = new ArrayList<>(phoneMapDetailList);
		if(arrayList.size()>0) {
			List arrayList_hava = baseSite.getMap(arrayList);
			if(arrayList_hava!=null && arrayList_hava.size()>0) {
				for(PhoneMapDetail detail:(List<PhoneMapDetail>)arrayList_hava) {
					for(PhoneRecord pr:pageCall.getContent()) {
						if(StringUtils.isEmpty(pr.getAddress()) && StringUtils.isNotEmpty(pr.getCid()) && StringUtils.isNotEmpty(pr.getLac())) {
							if(pr.getCid().equals(detail.getCID()) && pr.getLac().equals(detail.getLAC())) {
								pr.setAddress(detail.getADDRESS());
							}
						}
					}
				}
			}
		}
	}
	private boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * D3封装数据
	 * @param csp
	 * @param result
	 * @param css
	 */
	public Map<String, Object> getD3Result(List<EachOtherCallAnalysis> ecas) {
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> links = new ArrayList<Map<String,Object>>();
		
	    //封装对象
	    for(EachOtherCallAnalysis eca:ecas) {
	    	HashMap<String, Object> node = new HashMap<>();
		    node.put("id",UUID.randomUUID().toString());
		    node.put("name",eca.getSuspectPhone());
	    	node.put("width",60);
	    	node.put("height",60);
	    	node.put("image","/plugins/d3/image/dh.png");
		    nodes.add(node);
	    }
	    
	    //封装连线
	    for(EachOtherCallAnalysis eca:ecas) {
	    	String name1 = eca.getSuspectPhone();
	    	for(EachOtherCallAnalysisPersonal ecap : eca.getEcaps()) {
				if (ecap.getCallNumber() != 0) {
					String lineText = "通话" + ecap.getCallNumber() + "次，累计"+ecap.getCallSeconds()+"秒";
					String name2 = ecap.getSuspectPhonePersonal();
					String id1="";
					String id2="";
					boolean flag=true;
					for (Map<String, Object> m:nodes){
						if (name1.equals((String)m.get("name"))){
							id1=m.get("id").toString();
						}
						if (name2.equals((String)m.get("name"))){
							id2=m.get("id").toString();
						}
					}
					for(Map<String, Object> map:links) {
						if((map.get("node1").toString().equals(id1) && map.get("node2").toString().equals(id2))
								||(map.get("node1").toString().equals(id2) && map.get("node2").toString().equals(id1))) {
							flag=false;
							break;
						}
					}
					if(flag) {
						HashMap<String, Object> link = new HashMap<>();
						if(StringUtils.isNotEmpty(id1)) {
							link.put("node1",id1);
						}
						if(StringUtils.isNotEmpty(id2)) {
							link.put("node2",id2);
						}
						link.put("name",lineText);
						if(!link.keySet().contains("node1")||!link.keySet().contains("node2")) continue;
						links.add(link);
					}
				}
			}
	    }
		
		result.put("links", links);
		result.put("nodes", nodes);
		return result;
	}
}

